using System;
using System.Windows.Forms;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.WinApp.Popups;
using DevExpress.XtraEditors;
using InfoExpress.HIV.Domains;
using InfoExpress.HIV.Services;
using System.Data;

namespace InfoExpress.HIV.WinApp.Forms
{
  public partial class FrmDsDieuTri : XtraForm
  {
    private DataTable _dtbDsBenhNhan = null;
    private TList<UtDmdv> _donViList;

    public FrmDsDieuTri()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmDsDieuTri_Load(object sender, EventArgs e)
    {
      BindData();
      LoadData();
    }

    #region Command Handlers

    private void btnSearch_Click(object sender, EventArgs e)
    {
      pceSearch.ClosePopup();
      LoadData();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
      UIUtils.ClearTextEdit(txtHoTen);
      UIUtils.ClearTextEdit(txtMaThe);
      UIUtils.ClearOtherEdit(deTuNgay);
      UIUtils.ClearOtherEdit(deDenNgay);
      UIUtils.ClearOtherEdit(lueDonVi);
    }

    private void btnNew_Click(object sender, EventArgs e)
    {
      using (FrmChonBenhNhanDieuTri frm = new FrmChonBenhNhanDieuTri { Owner = this })
      {
        if (frm.ShowDialog() == DialogResult.OK)
        {
          using (FrmDkDieuTriOI frmDk = 
            new FrmDkDieuTriOI { Owner = this, 
              BenhNhanId = frm.BenhNhanId,
              MaBenhNhan = frm.MaBenhNhan, 
              NgayDangKy = frm.NgayDangKy })
          {
            if (frmDk.ShowDialog() == DialogResult.OK)
            {
              UIUtils.ShowInformationMessage(Constants.MsgInfoDsDieuTri_001);
              LoadData();
            }
          }
        }
      }
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        int benhNhanId = (int)gvDsBenhNhan.GetFocusedDataRow()["BenhNhanId"];
        BenhNhanDangKy benhNhan = 
          GlobalCache.BenhNhanDangKyService.GetByBenhNhanId(benhNhanId);

        using (FrmCapTheMoi frm = new FrmCapTheMoi(false, benhNhan) { Owner = this })
        {
          if (frm.ShowDialog() == DialogResult.OK)
          {
            try
            {
              GlobalCache.BenhNhanDangKyService.Save(frm.ThongTinBenhNhan);
              
              //For Non-Webservice
              //using (ConnectionScope.CreateTransaction())
              //{
              //  GlobalCache.BenhNhanDangKyService.Save(frm.ThongTinBenhNhan);
              //  if (ConnectionScope.Complete())
              //  {
              //    MainFormUtils.SetSavedStatus();
              //  }
              //  else
              //  {
              //    MainFormUtils.SetSavedFailedStatus();
              //    UIUtils.ShowErrorMessage(Constants.MsgErrSave);
              //  }
              //}
            }
            catch (Exception ex)
            {
              Logger.LogCritical("FrmDsDieuTri::btnEdit_Click", ex, "General", null);
              UIUtils.ShowCannotSaveDataMessage();
            }
          }
        }
      }
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        if (UIUtils.ShowQuestionMessage(Constants.MsgQuestionDelete) == DialogResult.Yes)
        {
          try
          {
            int dieuTriId = (int)gvDsBenhNhan.GetFocusedDataRow()["DieuTriId"];
            DieuTri dieuTri = GlobalCache.DieuTriService.GetById(dieuTriId);
            dieuTri.DeleteFlag = true;
            GlobalCache.DieuTriService.Save(dieuTri);

            //For Non-Webservice
            //using (ConnectionScope.CreateTransaction())
            //{
            //  int dieuTriId = (int)gvDsBenhNhan.GetFocusedDataRow()["DieuTriId"];
            //  DieuTri dieuTri = GlobalCache.DieuTriService.GetById(dieuTriId);
            //  dieuTri.DeleteFlag = true;
            //  GlobalCache.DieuTriService.Save(dieuTri);

            //  if (ConnectionScope.Complete())
            //  {
            //    gvDsBenhNhan.DeleteRow(gvDsBenhNhan.FocusedRowHandle);
            //    MainFormUtils.SetDeletedStatus();
            //  }
            //  else
            //  {
            //    MainFormUtils.SetDeletedFailedStatus();
            //    UIUtils.ShowErrorMessage(Constants.MsgErrDelete);
            //  }
            //}
          }
          catch (Exception ex)
          {
            Logger.LogCritical("FrmDsDieuTri::btnDelete_Click", ex, "General", null);
            UIUtils.ShowCannotDeleteDataMessage();
          }
        }
      }
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
    }

    #endregion

    #region Data Loading

    private void bwDataLoading_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
      DateTime? tuNgay = (deTuNgay.EditValue != null) ?
        (DateTime?)deTuNgay.EditValue : null;
      DateTime? denNgay = (deDenNgay.EditValue != null) ?
        (DateTime?)deDenNgay.EditValue : null;
      string donVi = (lueDonVi.EditValue != null) ?
        (string)lueDonVi.EditValue : null;

      try
      {
        DataSet dtsDsBenhNhan = 
          GlobalCache.DieuTriService.DsBenhNhan(donVi, txtMaThe.Text, txtHoTen.Text, 
            false, false, tuNgay, denNgay);
        _dtbDsBenhNhan = dtsDsBenhNhan.Tables[0];
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmDsDieuTri::bwDataLoading_DoWork", ex, "General", null);
      }
    }

    private void bwDataLoading_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();
      MainFormUtils.SetLoadedStatus();
      ToggleEditControls(true);

      if (_dtbDsBenhNhan != null)
      {
        gcDsBenhNhan.DataSource = _dtbDsBenhNhan;
      }
      else
      {
        MainFormUtils.SetLoadedFailedStatus();
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #endregion

    private void edit_Enter(object sender, EventArgs e)
    {
      UIUtils.SelectAll((BaseEdit)sender);
    }

    #endregion

    #region Helper Methods

    private void BindData()
    {
      _donViList = GlobalCache.DonViService.GetAll();
      lueDonVi.Properties.DataSource = _donViList;
    }

    private void LoadData()
    {
      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetLoadingStatus();
      ToggleEditControls(false);
      bwDataLoading.RunWorkerAsync();
    }

    private void ToggleEditControls(bool enabled)
    {
      btnSearch.Enabled = enabled;
      btnNew.Enabled = enabled;
      btnEdit.Enabled = enabled;
      btnDelete.Enabled = enabled;
      btnPrint.Enabled = enabled;
    }

    #endregion
    
  }
}